<template>
  <div class="login-container">
    <div class="mainArea">
      <div class="loginImg">
        <div class="line1">升学在线</div>
        <div class="line2">打造全国升学就业服务平台</div>
        <div class="line3" />
      </div>
      <div v-if="!choLoginType" class="login_1">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

          <div class="title-container">
            <div class="title_image">
              <img src="@/assets/login/logo.png" alt="">
            </div>
            <h3 class="title">高校工作智能办公平台</h3>
          </div>

          <el-form-item prop="username">
            <el-input
              ref="username"
              v-model="loginForm.username"
              placeholder="请输入账号"
              name="username"
              type="text"
              tabindex="1"
              auto-complete="on"
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
              :key="passwordType"
              ref="password"
              v-model="loginForm.password"
              :type="passwordType"
              placeholder="请输入密码"
              name="password"
              tabindex="2"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
          </el-form-item>
          <div :class="['btn', isCanClick ? 'btn_act' : '']" @click="handleLogin">登录</div>
          <div class="agreement">
            <el-checkbox v-model="checked" />
            同意成长在线
            <span>用户协议及隐私协议</span>
          </div>
          <!-- <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button> -->
          <!-- <div>
            <el-button type="info" style="width:100%;margin-bottom:30px;" @click="goMobileSystem()">访问手机版</el-button>
          </div> -->
        </el-form>
      </div>
      <div v-else class="login_2">
        <div class="title">请选择您要进入的平台</div>
        <div class="mainContent">
          <div v-show="division == 1" class="typeModel" @click="choTypeIntoIndex(1)">
            <div class="image"><img src="@/assets/login/login_pic_job.png" alt=""></div>
            <div class="name">就业信息管理中心</div>
          </div>
          <div class="typeModel" @click="choTypeIntoIndex(2)">
            <div class="image"><img src="@/assets/login/login_pic_school.png" alt=""></div>
            <div class="name">院校信息管理中心</div>
          </div>
        </div>
        <div class="goPrev" @click="goBackPrev">返回上一步</div>
      </div>
    </div>
    <div class="custom">遇到了问题？客服热线 027-88161985</div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入手机号'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能少于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      redirect: undefined,
      isCanClick: false,
      checked: true,
      division: null,
      choLoginType: false // 系统选择框弹出
    }
  },
  computed: {
    username: function() {
      return this.loginForm.username
    },
    password: function() {
      return this.loginForm.password
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    },
    checked(boolean) {
      if (boolean) {
        const { loginForm } = this
        if (loginForm.username === '' || loginForm.password === '') {
          this.$message({
            type: 'error',
            message: '请输入账号密码!'
          })
          this.checked = false
        } else {
          this.isCanClick = true
        }
      } else {
        this.isCanClick = false
      }
    },
    username: {
      handler(string) {
        if (string === '') {
          this.isCanClick = false
        } else if (this.checked && this.loginForm.password !== '') {
          this.isCanClick = true
        }
      }
    },
    password: {
      handler(string) {
        if (string === '') {
          this.isCanClick = false
        } else if (this.checked && this.loginForm.username !== '') {
          this.isCanClick = true
        }
      }
    }
  },
  created() {
    if (this.checked && this.loginForm.password !== '') {
      this.isCanClick = true
    }
    this.division = process.env.VUE_APP_DEV_DIVISION
  },
  mounted() {
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      if (this.isCanClick) {
        const loading = this.$loading({
          lock: true,
          text: '加载中',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.$store.dispatch('user/login', this.loginForm).then(() => {
              this.choLoginType = true
            }).catch(() => {
            }).finally(() => {
              loading.close()
            })
          } else {
            console.log('error submit!!')
            loading.close()
            return false
          }
        })
      }
    },
    goMobileSystem() {
      window.location.href = process.env.VUE_APP_MOBILE_SYSTEM
    },
    async logout() {
      localStorage.removeItem('system')
      await this.$store.dispatch('user/logout')
      // this.$router.push(`/login?redirect=${this.$route.fullPath}`)
      this.$router.push('/login')
    },
    // 点击'返回上一步'
    goBackPrev() {
      this.choLoginType = false
      this.logout()
      this.loginForm = {
        username: '',
        password: ''
      }
      this.checked = false
    },
    choTypeIntoIndex(type) {
      if (type === 1) {
        localStorage.setItem('system', 'new')
      } else {
        localStorage.setItem('system', 'old')
      }
      this.$router.push({ path: this.redirect || '/' })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;
$default_font_color: #0A2045;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 20px 0 17px 0;
      color: $default_font_color;
      height: 47px;
      caret-color: $default_font_color;
      font-size: 14px;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px #fff inset !important;
        -webkit-text-fill-color: $default_font_color !important;
      }
    }
  }

  .el-form-item {
    border-bottom: 1px solid #E1E4EB;
    background: #fff;
    color: $default_font_color;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
$default_font_color: #0A2045;

.login-container {
  min-height: 100%;
  width: 100%;
  padding-top: 100px;
  background: url('../../assets/login/login_pic_bg.png') no-repeat center center;
  background-size: 100% 100%;
  overflow: hidden;
  position: relative;

  .mainArea {
    width: 1006px;
    margin: 0 auto;
    display: flex;
    justify-content: flex-start;
    .loginImg {
      width: 446px;
      height: 620px;
      background: url('../../assets/login/login_pic_bg_small.png') no-repeat center center;
      background-size: 100% 100%;
      padding: 129px 0 0 54px;

      .line1 {
        color: #fff;
        font-size: 38px;
        font-weight: bold;
        line-height: 50px;
        margin-bottom: 18px;
      }
      .line2 {
        color: #fff;
        font-size: 26px;
        line-height: 35px;
        margin-bottom: 32px;
      }
      .line3 {
        width: 40px;
        height: 2px;
        background: #fff;
      }
    }
    .login_1 {
      width: 560px;
      height: 620px;
      background: #fff;
      border-radius:0px 20px 20px 0px;
      padding: 70px 90px 0 90px;

      .btn {
        width: 100%;
        height: 44px;
        border-radius: 10px;
        background: #E1E4EB;
        color: #fff;
        line-height: 44px;
        text-align: center;
        font-size: 14px;
        cursor: pointer;
      }
      .btn_act {
        background: #0c66ff;
      }
      .btn_act:hover {
        background: #0951CC;
      }

      .agreement {
        color: $default_font_color;
        font-size: 14px;
        line-height: 24px;
        margin-top: 24px;

        .el-checkbox {
          margin-right: 15px;
        }

        span {
          color: #0C66FF;
          text-decoration: underline;
          cursor: pointer;
        }
      }
    }
    .login_2 {
      width: 560px;
      height: 620px;
      background: #fff;
      border-radius:0px 20px 20px 0px;
      padding: 124px 90px 0 90px;

      .title {
        color: $default_font_color;
        font-size: 14px;
        line-height: 19px;
        margin-bottom: 50px;
      }

      .mainContent {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 80px;

        .typeModel {
          width: 160px;
          height: 204px;
          background: #fff;
          box-shadow:0px 4px 4px 0px rgba(10,32,69,0.12);
          border-radius: 10px;
          cursor: pointer;
          .image {
            width: 160px;
            height: 160px;
            img {
              width: 100%;
              height: 100%;
            }
          }
          .name {
            color: $default_font_color;
            font-size: 14px;
            line-height: 19px;
            text-align: center;

          }
        }

        .typeModel:hover {
          box-shadow:0px 13px 13px 0px rgba(0,0,0,0.12);
          border-radius: 8px;
        }
      }

      .goPrev {
        color: #98A1B1;
        font-size: 14px;
        line-height: 19px;
        cursor: pointer;
      }

    }
  }

  .custom {
    margin-top: 56px;
    color: $default_font_color;
    font-size: 14px;
    text-align: center;
    line-height: 19px;
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    // padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    text-align: center;

    .title_image {
      margin: 0 auto;
      width: 310px;
      height: 80px;
      img {
        width: 100%;
        height: 100%;
      }
    }

    .title {
      font-size: 18px;
      line-height: 24px;
      color: $default_font_color;
      margin: 40px auto 20px;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

}
</style>
